package com.wondertek.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wondertek.common.base.Result;

import com.wondertek.common.log.AutoLog;

import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.sys.req.BaseUpdateReq;
import com.wondertek.service.dto.sys.req.SysRoleAddReq;
import com.wondertek.service.dto.sys.req.SysRoleQueryReq;
import com.wondertek.service.dto.sys.req.SysRoleUpdateReq;
import com.wondertek.service.entity.sys.SysRolePo;
import com.wondertek.service.service.sys.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/role")
@Tag(name = "系统角色管理")
public class SysRoleController{

    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 分页查询角色
     * @param sysRoleQueryReq /
     * @return /
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询角色")
    @PreAuthorize("@el.check('backend:sys:role:page')")
    public Result<Page<SysRolePo>> page(SysRoleQueryReq sysRoleQueryReq){
        return Result.success(sysRoleService.page(sysRoleQueryReq));
    }

    /**
     * 查询所有角色
     * @return /
     */
    @GetMapping("/findList")
    @Operation(summary = "查询所有角色")
    public Result<List<SysRolePo>> list(){
        return Result.success(sysRoleService.findList());
    }

    /**
     * 用户id查询角色
     * @return /
     */
    @GetMapping("/findListByUserId")
    @Operation(summary = "用户id查询角色")
    public Result<List<SysRolePo>> findListByUserId(@RequestParam("userId") String userId){
        return Result.success(sysRoleService.findListByUserId(userId));
    }


    /**
     * 新增角色
     * @param sysRoleAddReq /
     * @return /
     */
    @PostMapping("/add")
    @Operation(summary = "新增角色")
    @AutoLog(category = "sys", handleType = "log.role.insert",
            handleNote = "'新增角色:'+#sysRoleAddReq.roleName")
    @PreAuthorize("@el.check('backend:sys:role:add')")
    public Result<?> add(@Validated @RequestBody SysRoleAddReq sysRoleAddReq){
        sysRoleService.add(sysRoleAddReq);
        return Result.success();
    }

    /**
     * 修改角色
     * @param sysRoleUpdateReq /
     * @return /
     */
    @PostMapping("/update")
    @Operation(summary = "修改角色")
    @AutoLog(category = "sys", handleType = "log.role.update",
            handleNote = "'修改角色:'+#sysRoleUpdateReq.roleName")
    @PreAuthorize("@el.check('backend:sys:role:update')")
    public Result<?> update(@Validated @RequestBody SysRoleUpdateReq sysRoleUpdateReq){
        sysRoleService.update(sysRoleUpdateReq);
        return Result.success();
    }

    /**
     * 禁用角色
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/disable")
    @Operation(summary = "禁用角色")
    @AutoLog(category = "sys", handleType = "log.role.disable",
            handleNote = "'禁用角色:'+#baseUpdateReq.id")
    @PreAuthorize("@el.check('backend:sys:role:disable')")
    public Result<?> disable(@RequestBody @Validated BaseUpdateReq baseUpdateReq){
        sysRoleService.disable(baseUpdateReq.getId());
        return Result.success();
    }

    /**
     * 启用角色
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/enable")
    @Operation(summary = "启用角色")
    @AutoLog(category = "sys", handleType = "log.role.enable",
            handleNote = "'启用角色:'+#baseUpdateReq.id")
    @PreAuthorize("@el.check('backend:sys:role:enable')")
    public Result<?> enable(@RequestBody @Validated BaseUpdateReq baseUpdateReq ){
        sysRoleService.enable(baseUpdateReq.getId());
        return Result.success();
    }

    /**
     * 删除角色
     * @return /
     */
    @PostMapping("/delete")
    @Operation(summary = "删除角色")
    @AutoLog(category = "sys", handleType = "log.role.delete",
            handleNote = "'删除角色'")
    @PreAuthorize("@el.check('backend:sys:role:delete')")
    public Result<?> delete(@RequestBody @Validated BaseUpdateReq baseUpdateReq){
        sysRoleService.delete(baseUpdateReq.getId());
        return Result.success();
    }

}
